<?php

class Control_IndexController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
        $this->SettingsDb = new Default_Model_Table_Settings();
        $this->view->CopyRight = ($this->SettingsDb->getCopyright());
        $this->dbTable1 = new Control_Model_Table_SubPage();
        $this->dbTable2 = new Control_Model_Table_Login();
        $this->yesButton = 'Yes'; //напис на кнопці Так під час видалення
        $this->noButton = 'No'; //напис на кнопці Ні під час видалення
        $this->view->controller = $this->getRequest()->getControllerName();
        $this->view->action = $this->getRequest()->getActionName();
    }

    public function indexAction() {
        $this->_helper->redirector('users');
    }

    public function adminMenuAction() {
        $this->view->subAbout = $this->dbTable1->getPagesToMenuWhere('1');
        $this->view->subServices = $this->dbTable1->getPagesToMenuWhere('2');
        $this->view->subNews = $this->dbTable1->getPagesToMenuWhere('3');
    }

    public function lockPageAction() {
// action body
    }

    public function checkLockAction() {
        $logout = $this->_getParam('logout', 0);
        if ($logout > 0) {
            $loginDb = new Control_Model_Table_Login();
            $loginDb->logout();
        }
        if ((isset($_POST['login']) && isset($_POST['password'])) or ($_POST['login'] == '' or $_POST['password'] == '')) {

            $login = new Control_Model_Table_Login();
            $login->login($_POST['login'], $_POST['password']);
            $this->_helper->redirector('index', 'index', 'control');
        }
    }

    public function usersAction() {

        $this->view->PortfolioData = $this->dbTable2->getAllUsers();
        $form = new Control_Model_Form_Register();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam = $this->_getParam('del', 0);
        ///---------------------------------------------------------------------//
        if ($this->getRequest()->isPost() && ($delparam != 1)) { //Якщо відправлена форма без видалення
            $formData = $this->getRequest()->getPost();
            if (($form->isValid($formData))&&($form->getValue('password1')==$form->getValue('password2'))) {
                $id = (int) $form->getValue('id');
                $user = $form->getValue('user');
                if ($form->getValue('password1')!=""){$password = md5($form->getValue('password1'));}
else {$password="";}
                $name = $form->getValue('Name');
                $soname = $form->getValue('Soname');
                $this->dbTable2->updateUser($id, $user, $password, $name, $soname);
                $this->_helper->redirector('users');
            } else {
                $form->populate($formData);
            }
        } elseif ($this->getRequest()->isPost() && ($delparam == 1)) { //Якщо відправлена форма з видаленням
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                if ($del == $this->yesButton) {
                    $id = $this->getRequest()->getPost('id');
                    $this->dbTable2->deleteUser($id);
                } else {
                    $this->_helper->redirector('users');
                }
                $this->_helper->redirector('users');
            }
        } else {//Виведення сторінки едіт
            $id = (int) $this->_getParam('id', 0);
            $del = (int) $this->_getParam('del', 0);
            $this->view->get_id = $id;
            if ($id > 0 && $del == 0) {
                $formdata = $this->dbTable2->getUser($id);
                if (isset($formdata)) {
                    $form->populate($formdata);
                } else {
                    $this->view->get_id = 0;
                };
            } elseif ($id > 0 && $del == 1) {
                $this->view->get_del = 1;
                $this->view->yesButton = $this->yesButton;
                $this->view->noButton = $this->noButton;
                $this->view->NameToDelete = $this->dbTable2->getUser($id);
            } elseif ($id == 0) {
                $this->view->get_id = $id;
            } else {
                $this->view->get_id = 0;
                echo "<span style='color: red;'>Error id: $id<br></span>";
            }
        }
    
    }

    public function userAddAction() {
        $form = new Control_Model_Form_Register();
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if (($form->isValid($formData))&&($form->getValue('password1')==$form->getValue('password2'))&&($form->getValue('password1')!='')) {
                $user = $form->getValue('user');
                $password = md5($form->getValue('password1'));
                $name = $form->getValue('Name');
                $soname = $form->getValue('Soname');
                $this->dbTable2->addUser($user, $password, $name, $soname);
                $this->_helper->redirector('users');
            } else {
                $form->populate($formData);
            }
        }
    }

}

